package com.woniuxy.microsportsserver.devicemag.service.impl;

import com.woniuxy.microsportsserver.bean.DeviceInfo;
import com.woniuxy.microsportsserver.devicemag.dao.DeviceRepository;
import com.woniuxy.microsportsserver.devicemag.service.IDeviceService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DeviceImpl implements IDeviceService {
    @Resource
    private DeviceRepository deviceRepository;

    @Override
    public void saveMovie(DeviceInfo deviceInfo) {
        deviceRepository.save(deviceInfo);
    }

  /*  @Override
    public void updateMovie(DeviceInfo DeviceInfo) {
        deviceRepository.save(movieInfo);
    }

    @Override
    public void deleteMovie(DeviceInfo DeviceInfo) {
        deviceRepository.delete(movieInfo);
    }
*/
//    @Override
//    public List<MovieInfo> findAll() {
//        return (List<MovieInfo>)movieRepository.findAll();
//    }
//
//    @Override
//    public MovieInfo findById(Long id) {
//        if (movieRepository.existsById(id)){
//
//        }
//        return null;
//    }
    @Override
    public List<DeviceInfo> findByParams(String deviceName, String deviceType, String deviceDesc) {
        return deviceRepository.findByDeviceNameOrDeviceTypeOrDeviceDescOrderByIdDesc(deviceName, deviceType, deviceDesc);
    }
    @Override
    public Iterable<DeviceInfo> search(String text) {
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //根据电影名，导演名，演员列表，电影类型，电影描述
        queryBuilder.withQuery(QueryBuilders.multiMatchQuery(text,"deviceName",
                "deviceType","deviceBrand","deviceDesc"));
        return deviceRepository.search(queryBuilder.build());
    }
    @Override
    public Page<DeviceInfo> search(String text, Pageable pageable) {
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //根据电影名，导演名，演员列表，电影类型，电影描述
        queryBuilder.withQuery(QueryBuilders.multiMatchQuery(text,"deviceName",
                "deviceType","deviceBrand","deviceDesc"));
        queryBuilder.withPageable(pageable);//设置分页参数
        return deviceRepository.search(queryBuilder.build());
    }
    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @Override
    public DeviceInfo findById(Long id) {
        if(deviceRepository.existsById(id)){
            return deviceRepository.findById(id).get();
        }
        return null;
    }
    /**
     * 查询所有
     * @return
     */
    @Override
    public Iterable<DeviceInfo> findAll() {
        return deviceRepository.findAll();
    }
    @Override
    public void saveMovies(List<DeviceInfo> deviceInfos) {
        deviceRepository.saveAll(deviceInfos);
    }

}
